/* 
 Copyright 2007 Simon Lebettre
 This file is part of BeanCooker.

 BeanCooker is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.

 BeanCooker is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>
 */ 
package net.sf.beancooker.dataproviders;

import java.util.Calendar;
import java.util.Random;


/**
 * RandomDataProvider gives you random values.
 * 
 * String are made of 3 to 10 a-z letter
 * 
 */
@SuppressWarnings("unchecked")
public class RandomDataProvider extends BaseDataProvider {
	
	private Random random = new Random();

	/**
	 * Instantiates a new random data provider.
	 */
	public RandomDataProvider() {
		setCalendar(Calendar.getInstance());
	}
	
	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getArraysSize()
	 */
	@Override
	public int getArraysSize() {
		int i = random.nextInt(4) + 1;// arrays size from 1 to 5		
		return i;
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getBooleanValue()
	 */
	@Override
	public Boolean getBooleanValue() {
		return random.nextBoolean();

	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getByteValueString()
	 */
	@Override
	public String getByteValueString() {

		return "" + random.nextInt(128);
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getCalendar()
	 */
	@Override
	public Calendar getCalendar() {
		// this has effect on getdate and getxmlcalendar too
		Calendar c = super.getCalendar();
		c.add(Calendar.DAY_OF_YEAR, random.nextInt());
		return c;
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getCharValue()
	 */
	@Override
	public char getCharValue() {
		return (char) random.nextInt(8);
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getDecimalValueString()
	 */
	@Override
	public String getDecimalValueString() {
		return "" + random.nextFloat();
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getEnumValue(java.lang.Class)
	 */
	@Override
	public Object getEnumValue(Class clazz) {
		Object[] enumConstants = clazz.getEnumConstants();
		return enumConstants[random.nextInt(enumConstants.length)];
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getIntegerValueString()
	 */
	@Override
	public String getIntegerValueString() {
		// limited to 16 bits because we use this for shorts too
		return "" + random.nextInt(32768);
	}

	/* (non-Javadoc)
	 * @see net.sf.beancooker.dataproviders.BaseDataProvider#getStringValue()
	 */
	@Override
	public String getStringValue() {

		return getRandomstring(3, 10);
	}

	/**
	 * generate random string with only a...z letters <br>
	 * and with a random but limited size
	 * 
	 * @param minSize minimum size
	 * @param maxSize maximum size
	 * 
	 * @return the random string
	 */
	public String getRandomstring(int minSize, int maxSize) {
		int n = getRandomInt(minSize, maxSize);
		byte b[] = new byte[n];
		for (int i = 0; i < n; i++) {
			b[i] = (byte) getRandomInt('a', 'z');
		}
		return new String(b);
	}

	/**
	 * generate random int.
	 * 
	 * @param lo the minimum value
	 * @param hi the max value
	 * 
	 * @return the random int
	 */
	public int getRandomInt(int lo, int hi) {
		int n = hi - lo + 1;
		int i = random.nextInt() % n;
		if (i < 0) i = -i;
		return lo + i;
	}

}
